package com.example.mydanmudemo

import android.content.Context
import android.view.View
import android.widget.*

class MoveView(context: Context?, danmuBean: DanmuBean) : LinearLayout(context) {
    private var ivAvator: ImageView? = null
    private var tvContent: TextView? = null
    private var itemView: LinearLayout? = null

    private var height:Int?=0
    private fun init(danmuBean: DanmuBean) {
        itemView = rootView?.findViewById(R.id.itemView)
        ivAvator = rootView?.findViewById(R.id.avator)
        tvContent = rootView?.findViewById(R.id.tv_content)
        ivAvator?.setImageResource(danmuBean.headImg)
        tvContent?.text = danmuBean.content
        itemView?.setOnClickListener {
            Toast.makeText(context,"点击了${danmuBean.content}",Toast.LENGTH_SHORT).show()
        }
        height= itemView?.display?.height
    }

    /**
     * 设置随机出现垂直位置
     */
    fun randomVerticalPos(heightPixels: Int) {
        val randonTop = (Math.random() * heightPixels).toInt()
        val params = layoutParams as RelativeLayout.LayoutParams
        params.topMargin = randonTop
        layoutParams = params
    }

    init {
       var rootView = View.inflate(context, R.layout.item, null)
        addView(rootView)
        init(danmuBean)
    }
}